package com.hx.voice.recognition.common.handler;

import com.hx.voice.recognition.common.constant.CommonConstants;
import com.hx.voice.recognition.common.exception.BaseException;
import com.hx.voice.recognition.common.exception.auth.ClientTokenException;
import com.hx.voice.recognition.common.exception.auth.UserInvalidException;
import com.hx.voice.recognition.common.exception.auth.UserTokenException;
import com.hx.voice.recognition.common.msg.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @descripe：全局切面异常
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/11 14:58
 * @version：V1.0
 */
@ResponseBody
@ControllerAdvice("com.hx.zjb.security")
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(ClientTokenException.class)
    public BaseResponse clientTokenExceptionHandler(HttpServletResponse response, ClientTokenException ex)
    {
        response.setStatus(403);
        log.error(ex.getMessage(),ex);
        return new BaseResponse(ex.getStatus(),ex.getMessage());
    }

    @ExceptionHandler(UserTokenException.class)
    public BaseResponse userTokenExceptionHandler(HttpServletResponse response, UserTokenException ex)
    {
        response.setStatus(200);
        log.error(ex.getMessage(),ex);
        return new BaseResponse(ex.getStatus(),ex.getMessage());
    }

    @ExceptionHandler(UserInvalidException.class)
    public BaseResponse userInvalidExceptionHandler(HttpServletResponse response, UserInvalidException ex)
    {
        response.setStatus(200);
        log.error(ex.getMessage(),ex);
        return new BaseResponse(ex.getStatus(),ex.getMessage());
    }

    @ExceptionHandler(BaseException.class)
    public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseException ex)
    {
        response.setStatus(500);
        log.error(ex.getMessage(),ex);
        return new BaseResponse(ex.getStatus(),ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse otherExcetptionHandler(HttpServletResponse response, Exception ex)
    {
        response.setStatus(500);
        log.error(ex.getMessage(),ex);
        return new BaseResponse(CommonConstants.EX_OTHER_CODE,ex.getMessage());
    }














}